﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project_Origin
{
    public partial class PO_Arcade_Mode : Form
    {
        //最大值
        public int Max { get; set; }
        //最小值
        public int Min { get; set; }
        //关卡数
        public int NoNumber { get; set; }
        //积分
        public int Integration { get; set; }
        //单次积分数
        public int OneIntegration { get; set; }
        //猜测次数
        public int Times { get; set; }
        //随机值
        public int I { get; set; }
        //计数
        public int TimeNumber { get; set; }

        //初始化
        public PO_Arcade_Mode()
        {
            InitializeComponent();
            //第一局初始化
            this.NoNumber = 1;
            GameStart(this.NoNumber);
            //积分初始化
            this.Integration = 0;
            PO_Aracde_Int.Text = this.Integration.ToString();

            //开启定时器
            Game_Timer.Enabled = true;
            //时间间隔
            Game_Timer.Interval = 1000;

        }

        private Point mousePoint = new Point();


        public void closethis()
        {
            this.Dispose();
            this.Close();
        }

        //返回按钮
        private void PO_Button_Back_Click(object sender, EventArgs e)
        {
            PO_Confirm_Form pO_Confirm_Form = new PO_Confirm_Form();
            if (pO_Confirm_Form != null)
            {
                if (pO_Confirm_Form.IsDisposed)
                    pO_Confirm_Form = new PO_Confirm_Form();//如果已经销毁，则重新创建子窗口对象
                pO_Confirm_Form.closefather += new PO_Confirm_Form.childclose(this.closethis);
                pO_Confirm_Form.ShowDialog(this);
                pO_Confirm_Form.Focus();
            }
            else
            {
                pO_Confirm_Form = new PO_Confirm_Form();
                pO_Confirm_Form.closefather += new PO_Confirm_Form.childclose(this.closethis);
                pO_Confirm_Form.ShowDialog(this);
                pO_Confirm_Form.Focus();
            }
        }

        private void PO_Timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 0.3)
                this.Opacity -= 0.3;
            else
            {
                PO_Timer1.Stop();
                this.Close();
                Environment.Exit(Environment.ExitCode);
            }
        }

        private void PO_MixNum_Button_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void PO_Button_Exit_Click(object sender, EventArgs e)
        {
            PO_Timer1.Start();
        }

        private void PO_Arcade_Mode_MouseDown(object sender, MouseEventArgs e)
        {
            this.mousePoint.X = e.X;
            this.mousePoint.Y = e.Y;
        }

        private void PO_Arcade_Mode_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Top = Control.MousePosition.Y - mousePoint.Y + 13;
                this.Left = Control.MousePosition.X - mousePoint.X + 13;
            }
        }

        //确定按钮单击
        private void PO_Button_Deciside_Click(object sender, EventArgs e)
        {
            if (pO_B_TextBox1.Text != "")
            {
                int number = int.Parse(pO_B_TextBox1.Text);
                Game(number);
                pO_B_TextBox1.Clear();
            }
        }


        //****************************************************
        //华丽的分割线   游戏

        //初始化
        public void GameStart(int nonember)
        {
            //初始化范围
            this.Min = 1;
            this.Max = nonember * 100;
            //显示
            PO_MIN.Text = this.Min.ToString();  //最小值
            PO_MAX.Text = this.Max.ToString();  //最大值
            PO_Arcade_GK.Text = nonember.ToString();    //显示关卡
            //单局100分
            this.OneIntegration = 100;
            PO_Arcade_OneInt.Text = this.OneIntegration.ToString();
            //次数归零
            this.Times = 0;
            //随机生成一个数字
            Random random = new Random();
            this.I = random.Next(this.Min, this.Max);
            //初始化时间
            this.TimeNumber = GameTime(nonember);
            PO_Arcade_TimeNumber.Text = this.TimeNumber.ToString();
            //默认字体黑色
            PO_Arcade_TimeNumber.ForeColor = Color.Black;

            //定时器开始
            Game_Timer.Start();

            //字体初始化
            //字体初始化
            string AppPath = Application.StartupPath;
            PrivateFontCollection privateFonts = new PrivateFontCollection();
            privateFonts.AddFontFile(AppPath + @"\Resources\站酷快乐体.ttf");//加载字体
            Font font = new Font(privateFonts.Families[0], 20);
            PO_MAX.Font = font;
            PO_MIN.Font = font;
            pO_B_TextBox1.Font = font;
            PO_Arcade_Lable.Font = font;


        }




        //游戏计算
        public void Game(int number)
        {

            if (number >= this.Min && number <= this.Max)
            {
                if (number > this.I)
                {
                    //赋值
                    this.Max = number;
                    PO_MAX.Text = number.ToString();
                    //次数加一
                    this.Times++;
                    //扣除相应分数
                    this.OneIntegration -= Times;
                    PO_Arcade_OneInt.Text = this.OneIntegration.ToString();
                    //提示
                    PO_Arcade_Lable.Text = "数字太大了！";

                }
                else if (number < this.I)
                {
                    //赋值
                    this.Min = number;
                    PO_MIN.Text = number.ToString();
                    //次数加一
                    this.Times++;
                    //扣除相应分数
                    this.OneIntegration -= Times;
                    PO_Arcade_OneInt.Text = this.OneIntegration.ToString();
                    
                    //提示
                    PO_Arcade_Lable.Text = "数字太小了！";
                }
                else
                {
                    //次数加一
                    Times++;
                    //分数累加
                    this.Integration += this.OneIntegration;
                    PO_Aracde_Int.Text = this.Integration.ToString();
                    //启动提示
                    PO_If_Next_Stage next = new PO_If_Next_Stage(this.Integration,2);
                    Game_Timer.Stop();
                    next.ShowDialog();
                    if (next.Status == 1)
                        this.Close();
                    else
                        GameStart(++NoNumber);
                }
            }
            else
            {
                PO_Arcade_Lable.Text = "你是不会看范围嘛！";
            }
        }

        private void pO_B_TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果是回车
            if (e.KeyChar == Convert.ToChar(System.Windows.Forms.Keys.Enter))
            {
                if (pO_B_TextBox1.Text != "")
                {
                    int number = int.Parse(pO_B_TextBox1.Text);
                    Game(number);
                    pO_B_TextBox1.Clear();
                }
                
            }
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符
                }
            }
            
        }

        private void pO_B_TextBox1_Enter(object sender, EventArgs e)
        {
            ImeMode = ImeMode.Disable;
        }




        //*****************************************************
        //华丽的分割线  时间

        //timer响应事件
        private void Game_Timer_Tick(object sender, EventArgs e)
        {
            //显示时间
            --this.TimeNumber;
            PO_Arcade_TimeNumber.Text = this.TimeNumber.ToString();
            if (this.TimeNumber == 0)
            {
                PO_If_Next_Stage next = new PO_If_Next_Stage(this.Integration,1);
                Game_Timer.Stop();
                next.ShowDialog();
                
                if (next.Status == 1)
                    this.Close();
                else
                    GameStart(1);


            }
            else if (this.TimeNumber <= 10)//低于十秒字变红
            {
                PO_Arcade_TimeNumber.ForeColor = Color.Red;
            }
        }

        //计时器时间计算
        public int GameTime(int nonumber)
        {
            return 60 + ((nonumber - 1) * 10);
        }

        private void PO_Arcade_Mode_Load(object sender, EventArgs e)
        {
            string AppPath = Application.StartupPath;
            PrivateFontCollection privateFonts = new PrivateFontCollection();
            try
            {
                privateFonts.AddFontFile(AppPath + @"\Resources\站酷快乐体.ttf");//加载字体
                Font font = new Font(privateFonts.Families[0], 20);
                PO_MAX.Font = font;
                PO_MIN.Font = font;
                pO_B_TextBox1.Font = font;
                PO_Arcade_GK.Font = font;
                PO_Aracde_Int.Font = font;
                PO_Arcade_Lable.Font = font;
                PO_Arcade_OneInt.Font = font;
                PO_Arcade_TimeNumber.Font = font;
            }
            catch
            {
                MessageBox.Show("字体不存在或加载失败\n程序将以默认字体显示", "警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
